package com.huichenghe.xinlvsh01.slide;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Rect;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Handler;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.huichenghe.xinlvsh01.Adapter.RecyclerViewAdapter;
import com.huichenghe.xinlvsh01.Adapter.RecyclerViewAdapterForSavedDevice;
import com.huichenghe.xinlvsh01.Ble.BleDataForSettingParams;
import com.huichenghe.xinlvsh01.Ble.BleScanUtils;
import com.huichenghe.xinlvsh01.Ble.BluetoothLeService;
import com.huichenghe.xinlvsh01.Ble.DeviceConfig;
import com.huichenghe.xinlvsh01.Ble.LocalDeviceEntity;
import com.huichenghe.xinlvsh01.CustomView.MyProgressDialog;
import com.huichenghe.xinlvsh01.DbEntities.DeviceSaveData;
import com.huichenghe.xinlvsh01.DbEntities.LocalDataBaseUtils;
import com.huichenghe.xinlvsh01.R;
import com.huichenghe.xinlvsh01.Utils.LocalDataSaveTool;
import com.huichenghe.xinlvsh01.Utils.MyConfingInfo;
import com.huichenghe.xinlvsh01.Utils.MyToastUitls;
import com.huichenghe.xinlvsh01.Utils.RequestUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 设备管理界面
 */
public class DeviceAmdActivity extends AppCompatActivity
{
    public static final String TAG = DeviceAmdActivity.class.getSimpleName();
    private LinearLayout mRelativeLayout;     // 扫描列表布局
    private Rect myRect;
    private AppBarLayout mAppBarLayout;
    private FloatingActionButton searchButton;
    private RecyclerView mRecyclerView;
    private int SHOW_FINST_PAGER = 1;         // 代表第一布局
    private int SHOW_SECOND_PAGER = 2;        // 代表第二布局
    private int showState = SHOW_FINST_PAGER; // 标识变量,标识当前显示的布局
    private ArrayList<LocalDeviceEntity> myData;    // 扫描的设备集合
    private List<DeviceSaveData> drivaceDate;
    private RecyclerViewAdapter deviceAdapter;  // 扫描结果显示的adapter
    private RecyclerViewAdapterForSavedDevice saveDeviceAdapter;// 保存的设备的adapter
    private ContentLoadingProgressBar scanProgress;
    private MyProgressDialog connectProgress;
    private Handler mHandler = new Handler();
    private boolean hasUpdate;
//    private TextView buttonC;
//    private ImageView imageV;
    private RecyclerView mRecyclerViewSave;


    float cxx;
    float cyy;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
//        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        setContentView(R.layout.activity_device_amd);

        init();
        registerReceiverForAllEvent();
        selecterFormDBAndUpdateUI();    // 查询数据库，并显示
        Log.i("", "走了这里：：：");
        if(Build.VERSION.SDK_INT >= 23)
        {
            RequestUtils.requestPermission(DeviceAmdActivity.this);
            RequestUtils.requestPermissionSdcard(DeviceAmdActivity.this);
        }

    }



    @Override
    protected void onResume() {
        super.onResume();
//        mHandler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                showTheInvisibleView(mAppBarLayout);
//            }
//        }, 300);


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myReceiver);
    }


    /**
     * 查询数据库并更新UI
     */
    public void selecterFormDBAndUpdateUI()
    {
        drivaceDate.clear();
        List<DeviceSaveData> dataForDevice = LocalDataBaseUtils.getInstance(DeviceAmdActivity.this).getDeviceListFromDB();

        if(dataForDevice.size() == 0)
        {
            return;
        }
        for (int i = dataForDevice.size() - 1; i >= 0; i --)
        {
            drivaceDate.add(dataForDevice.get(i));
        }
        saveDeviceAdapter.notifyDataSetChanged();
    }

    /**
     * 注册广播
     */
    private void registerReceiverForAllEvent() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(DeviceConfig.DEVICE_CONNECTE_AND_NOTIFY_SUCESSFUL);
        filter.addAction(MyConfingInfo.ON_DEVICE_STATE_CHANGE);
        registerReceiver(myReceiver, filter);
    }

    private void init()
    {
        drivaceDate = new ArrayList<DeviceSaveData>();
        saveDeviceAdapter = new RecyclerViewAdapterForSavedDevice(DeviceAmdActivity.this, drivaceDate );
        saveDeviceAdapter.setOnItemInnerButtonClickListener(
                new RecyclerViewAdapterForSavedDevice.OnItemInnerButtonClickListener() {
            @Override
            public void OnClickViewOne(int position, View vBt, View vIm) {
//                DeviceAmdActivity.this.buttonC = (TextView)vBt;
//                DeviceAmdActivity.this.imageV = (ImageView)vIm;
//                Toast.makeText(DeviceAmdActivity.this, "连接", Toast.LENGTH_SHORT).show();
                // 从集合中取出当前设备对象
                DeviceSaveData deviceSaveData = drivaceDate.get(position);

                // 构建连接需要的实体类
                LocalDeviceEntity deviceEntity = new LocalDeviceEntity(
                        deviceSaveData.getDeviceName(),
                        deviceSaveData.getDeviceAddress(),
                        -50,
                        new byte[]{});
                showProgressDialog(deviceEntity.getName());
                // 连接设备,并更新UI
//                connecteToTargetDevice(deviceEntity, position, true);
                BluetoothLeService.getInstance().connect(deviceEntity);
                // 连接10秒钟，若没有连接成功，则提示连接失败
                mHandler.postDelayed(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        if(!BluetoothLeService.getInstance().isConnectedDevice())
                        {
                            mHandler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    closeProgressDialog();
                                    BluetoothLeService.getInstance().close(false);
                                }
                            }, 1000);

                            connectProgress
                                    .changeTheImageView(R.drawable.disconnect);
                            connectProgress.changeTheText(R.string.connecte_device_failed);

                        }
                    }
                }, 10000);




            }

            @Override
            public void OnClickViewTwo(int position, View vBt, View vIm) {
//                DeviceAmdActivity.this.buttonC = (TextView)vBt;
//                DeviceAmdActivity.this.imageV = (ImageView)vIm;
                // 从集合中取出当前设备对象
                DeviceSaveData deviceSaveData = drivaceDate.get(position);

                LocalDeviceEntity deviceEntity = new LocalDeviceEntity(
                        deviceSaveData.getDeviceName(),
                        deviceSaveData.getDeviceAddress(),
                        -50,
                        new byte[]{}
                        );
                // 判断当前设备是否连接
                Boolean isCon = BluetoothLeService.getInstance().isDeviceConnected(deviceEntity);
                if(isCon)
                {   // 若是连接状态，则断开
                    BluetoothLeService.getInstance().close(false);

//                    long time = System.currentTimeMillis();
//                    Date date = new Date();
//                    date.setTime(time);
//                    LocalDataBaseUtils.getInstance(DeviceAmdActivity.this)
//                            .insertDeviceList(date,
//                                    deviceSaveData.getDeviceName(),
//                                    deviceSaveData.getDeviceAddress());
//                    drivaceDate.clear();
//
//                    selecterFormDBAndUpdateUI();
//                    hasUpdate = true;
                }
                else
                {   // 若是断开状态，则删除
                    LocalDataBaseUtils.getInstance(DeviceAmdActivity.this)
                                .delete(DeviceSaveData.class, "deviceName", deviceSaveData.getDeviceName());
                    drivaceDate.remove(position);
                    saveDeviceAdapter.removeItem(position);
                    MyToastUitls.showToast(DeviceAmdActivity.this, R.string.device_already_delete, 2);
                }


//                Toast.makeText(DeviceAmdActivity.this, "断开", Toast.LENGTH_SHORT).show();
            }
        });


        // 显示已连接过的设备的RecyclerView
        mRecyclerViewSave = (RecyclerView)findViewById(R.id.recycler_view_save);
        mRecyclerViewSave.setHasFixedSize(true);
        mRecyclerViewSave.setItemAnimator(new DefaultItemAnimator());
        mRecyclerViewSave.setLayoutManager(new LinearLayoutManager(DeviceAmdActivity.this));
        mRecyclerViewSave.setAdapter(saveDeviceAdapter);




        scanProgress = (ContentLoadingProgressBar)findViewById(R.id.scan_progress);
        myData = new ArrayList<LocalDeviceEntity>();
        deviceAdapter = new RecyclerViewAdapter(DeviceAmdActivity.this, myData);
        deviceAdapter.setOnScanDeviceItemClickListener(myItemListener);
          // 导航条透明
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
           //底部虚拟键透明
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//        shadow = (TextView)findViewById(R.id.dialog_show_shadow);
        TextView closeDialog = (TextView)findViewById(R.id.scan_result_show_close);
        closeDialog.setOnClickListener(myListener);
        mAppBarLayout = (AppBarLayout)findViewById(R.id.appbar_layout_big);
        mRelativeLayout = (LinearLayout)findViewById(R.id.scan_device_show);
        // toolbar
        Toolbar toolbar = (Toolbar)findViewById(R.id.device_toolbar);
        toolbar.setTitle(getString(R.string.connect_to_device));
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        // 返回键监听
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DeviceAmdActivity.this.finish();      // 结束页面
            }
        });

        // FloatingActionButton,搜索按钮
        searchButton = (FloatingActionButton)findViewById(R.id.seach_button);
        searchButton.setOnClickListener(myListener);

        // 显示扫描结果的 RecyclerView
        mRecyclerView = (RecyclerView)findViewById(R.id.Recycler_view);
        mRecyclerView.setHasFixedSize(true);    // 方法用来使RecyclerView保持固定的大小
        // 添加分割线
//        mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
//            @Override
//            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
//                super.onDraw(c, parent, state);
//            }
//        });

        // 设置嵌套滑动为false，即RevyclerView滑动是，同布局内的CollapsingToolbarLayout不同步滑动
        // 此设置的原因是RecyclerView内部默认设置为true，会联动滑动，所以特此设置
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());// 设置item添加移除动画
        mRecyclerView.setLayoutManager(new LinearLayoutManager(DeviceAmdActivity.this));
        mRecyclerView.setAdapter(deviceAdapter);


    }

    View.OnClickListener myListener = new View.OnClickListener()
    {
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onClick(View v)
        {
            switch (v.getId())
            {
                case R.id.scan_result_show_close:

//                    hideTheSecondLayout();      // 隐藏扫描结果布局
                    if(Build.VERSION.SDK_INT < 21)
                    {
                        mRelativeLayout.setVisibility(View.INVISIBLE);
                        showState = SHOW_FINST_PAGER;
                    }
                    else
                    {
                        hideTheSecondLayout();
                    }

                    break;

                case R.id.seach_button:
                    // 点击按钮搜索设备
                    if(Build.VERSION.SDK_INT < 21)
                    {
                        mRelativeLayout.setVisibility(View.VISIBLE);
                        showState = SHOW_SECOND_PAGER;
                    }
                    else
                    {
                        showTheFirstLayout();       // 显示扫描结果布局
                    }
                    new searchAsycnTask().execute();
                    BleScanUtils.getBleScanUtilsInstance(DeviceAmdActivity.this).setmOnDeviceScanFoundListener(myDeviceListnenr);
                break;
            }
        }
    };




    /**
     * 实例化设备监听器，并对扫描到的设备进行监听
     */
    BleScanUtils.OnDeviceScanFoundListener myDeviceListnenr = new BleScanUtils.OnDeviceScanFoundListener()
    {
        @Override
        public void OnDeviceFound(LocalDeviceEntity mLocalDeviceEntity) {
            boolean isEquals = false;
            Log.i("", "扫描到的设备" + mLocalDeviceEntity.getName() + "  " + mLocalDeviceEntity.getRssi());
            if(mLocalDeviceEntity != null)
            {
                //遍历之前的设备，过滤重复的设备
                if(myData != null && myData.size() > 0)
                {
                    for (int i = 0; i < myData.size(); i++)
                    {
                        LocalDeviceEntity De = myData.get(i);
                        if(De.equals(mLocalDeviceEntity))
                        {
                            isEquals = true;    // 有相同，设为true
                        }
                    }

                    if(!isEquals)               // 不相同则添加到数据集合
                    {
                        myData.add(mLocalDeviceEntity);
                    }
                }
                if(myData.size() == 0)          // 当扫描到第一个设备，直接添加
                {
                    myData.add(mLocalDeviceEntity);
                }
                deviceAdapter.notifyDataSetChanged();

            }

        }
    };



    class searchAsycnTask extends AsyncTask<Void, Void, Boolean>
    {

        @Override
        protected Boolean doInBackground(Void... params) {

            // 扫描设备
            BleScanUtils.getBleScanUtilsInstance(DeviceAmdActivity.this).scanLeDeviceLoopBegin(true, false, scanProgress);
            return null;
        }
    }

    /**
     * 显示第二布局，并执行动画
     */

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void showTheFirstLayout()
    {
        //                  searchButton.setOnTouchListener(new View.OnTouchListener() {
//                      @Override
//                      public boolean onTouch(View v, MotionEvent event) {
//                         cxx = event.getRawX();
//                         cyy = event.getRawY() * 2;
//                          return false;
//                      }
//                  });

//                int[] location = new int[2];
//                searchButton.getLocationInWindow(location);
//                int x = location[0];
//                int y = location[1];
//                    shadow.setVisibility(View.VISIBLE);


        // FloatingButton隐藏

        cxx = (int) searchButton.getX() + (searchButton.getWidth()/2);
        cyy = mAppBarLayout.getMeasuredHeightAndState() * 2;
//                int cyyy = searchButton.getHeight();


        // get the final radius for the clipping circle 最大的作为半径
        int finalRadius = Math.max(mRelativeLayout.getWidth(), mRelativeLayout.getHeight());

        // create the animator for this view (the start radius is zero)
        Animator anims =
                ViewAnimationUtils.createCircularReveal(mRelativeLayout, (int) cxx, (int) cyy / 2, 0, finalRadius);

        // make the view visible and start the animation
        mRelativeLayout.setVisibility(View.VISIBLE);
        anims.start();
//                searchButton.setVisibility(View.GONE);
        searchButton.hide();
        showState = SHOW_SECOND_PAGER;
    }

    /**
     * 显示隐藏的布局，并执行圆形动画
     * @param v
     */
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void showTheInvisibleView(View v)
    {
        float x = v.getWidth()/2;
        float y = v.getHeight() * 2;
        int theRadius = Math.max(v.getWidth(), v.getHeight());

        ViewAnimationUtils.createCircularReveal(v, (int)x, (int)y, 0, theRadius);
        v.setVisibility(View.VISIBLE);

    }

    /**
     * 隐藏第二布局,并执行动画
     */
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void hideTheSecondLayout() {
        // get the center for the clipping circle
        // 获取园的中心
        cxx = (int)searchButton.getX() + (searchButton.getWidth()/2);
        cyy = mAppBarLayout.getMeasuredHeightAndState();
//                    cyy = searchButton.getHeight() * 2;

        // get the initial radius for the clipping circle
        // 获取园的半径
        int initialRadius = mRelativeLayout.getWidth();

        // create the animation (the final radius is zero)
        Animator anim =
                ViewAnimationUtils.createCircularReveal(mRelativeLayout, (int)cxx, (int)cyy, initialRadius, 0);

        // make the view invisible when the animation is done
        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                mRelativeLayout.setVisibility(View.INVISIBLE);

//                            searchButton.show();


            }
        });
        // start the animation
        anim.start();
        showState = SHOW_FINST_PAGER;
    }

    /**
     * 保存连接成功的设备
     * @param deviceEntity
     */
    public void saveTheTargetDevice(LocalDeviceEntity deviceEntity)
    {

            // 当前时间

            long d = System.currentTimeMillis();
            Date connecteDate = new Date();
            connecteDate.setTime(d);

            // 当前的毫秒值是：1446621538047
            Log.i("", "当前的毫秒值是：" + d);


            // 先删除此条数据，再存入新数据，为的是添加到数据库最后一条

            DeviceSaveData de = LocalDataBaseUtils.getInstance(DeviceAmdActivity.this).selectDevice(deviceEntity.getName());
            if(de != null)
            {   // 设备存在，则删除，再添加，
                Log.i(TAG, "存储的设备名：" + de.getDeviceName());
                LocalDataBaseUtils.getInstance(DeviceAmdActivity.this).delete(DeviceSaveData.class, "deviceName", deviceEntity.getName());
                String change = LocalDataSaveTool.getInstance(DeviceAmdActivity.this).readSp(MyConfingInfo.PERSION_HAS_CHANGER);
                if(change.equals("1"))// 资料修改了
                {
                    new BleDataForSettingParams(DeviceAmdActivity.this).settingTheStepParamsToBracelet();
                }


            }
            else
            {
                new BleDataForSettingParams(DeviceAmdActivity.this).settingTheStepParamsToBracelet();
            }


            // 连接成功，将此设备存入数据库
            LocalDataBaseUtils.getInstance(DeviceAmdActivity.this).insertDeviceList
                    (connecteDate, deviceEntity.getName(), deviceEntity.getAddress());
            // 更新ui RecyclerVew显示

//             udateSaveDeviceList(deviceEntity.getName());


//            selecterFormDBAndUpdateUI();


    }


    /**
     * adapter内部对OnClick的监听，即对item的监听
     */
    RecyclerViewAdapter.OnScanDeviceItemClickListener myItemListener =
                    new RecyclerViewAdapter.OnScanDeviceItemClickListener()
    {
        @Override
        public void OnItemClick(int position)
        {
//            Toast.makeText(DeviceAmdActivity.this, "点击的是：第" + position + "个", Toast.LENGTH_SHORT ).show();
            LocalDeviceEntity deviceEntity = myData.get(position);// 获取设备实体类
            showProgressDialog("" + deviceEntity.getName());
            Log.i("", "点击的设备：" + deviceEntity.getName() + BluetoothLeService.getInstance());

            BluetoothLeService.getInstance().connect(deviceEntity);
        }
    };


    /**
     * 更新设备界面
     * 此position为扫描列表的position，非保存列表的position
     */
    private void udateSaveDeviceList(String deviceName) {
        // 通过连接的设备名查询数据库
        DeviceSaveData deviceData = LocalDataBaseUtils.getInstance(DeviceAmdActivity.this).selectDevice(deviceName);
        Log.i("", "查询的设备数据：" + deviceData.getDeviceName());

//        int index = drivaceDate.indexOf(deviceData);    // 返回此对象在集合中的下标

        if(drivaceDate.size() != 0)
        {
            for (int i = 0; i < drivaceDate.size(); i ++ )
            {
                if(drivaceDate.get(i).getDeviceName().equals(deviceData.getDeviceName()))
                {
                    drivaceDate.remove(i);
                }
            }
        }
            drivaceDate.add(0, deviceData);               // 将该条数据添加到显示列表的第一个
            saveDeviceAdapter.notifyDataSetChanged();
    }


    /**
     * 显示dialog
     * @param msg
     */
    public void showProgressDialog(String msg)
    {
        if(connectProgress == null)
        {
            connectProgress = new MyProgressDialog(DeviceAmdActivity.this, msg);
        }

        if(!connectProgress.isShowing())
        {
            connectProgress.show();
        }
    }


    /**
     * 关闭dialog
     */
    public void closeProgressDialog()
    {
        if(connectProgress != null && connectProgress.isShowing())
        {
            connectProgress.dismiss();
            connectProgress = null;
        }
    }

    /**
     * 广播接收器
     */
    BroadcastReceiver myReceiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, final Intent intent)
        {
            switch (intent.getAction())
            {   // 连接并使能的广播
                case DeviceConfig.DEVICE_CONNECTE_AND_NOTIFY_SUCESSFUL:
                    mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            closeProgressDialog();
                            LocalDeviceEntity device = (LocalDeviceEntity)intent.getSerializableExtra("DEVICE_OK_OBJ");
                            saveTheTargetDevice(device);
                            selecterFormDBAndUpdateUI();


                        }
                    }, 1000);
                    // 改变进度条的图片为对号
                    // 改变进度条的msg为连接成功
                    connectProgress.changeTheImageView(R.drawable.connect);
                    connectProgress.changeTheText(R.string.device_connecte_successful);
//                    if(buttonC != null && imageV != null)
//                    {
//                        buttonC.setVisibility(View.GONE);
//                        imageV.setVisibility(View.VISIBLE);
//                    }

                    break;

                case MyConfingInfo.ON_DEVICE_STATE_CHANGE:
                    // 断开后发送此广播，在此接收
//                    if(buttonC != null && imageV != null)
//                    {
//                        buttonC.setVisibility(View.VISIBLE);
//                        imageV.setVisibility(View.GONE);
//                    }
                    if(!BluetoothLeService.getInstance().isConnectedDevice())
                    {
                        selecterFormDBAndUpdateUI();
                        MyToastUitls.showToast(DeviceAmdActivity.this, R.string.device_already_disconnecte, 2);
                    }
//                        LocalDeviceEntity device = (LocalDeviceEntity)intent.getSerializableExtra("DEVICE_OBJ");
//                        long time = System.currentTimeMillis();
//                        Date date = new Date();
//                        date.setTime(time);
//                        LocalDataBaseUtils.getInstance(DeviceAmdActivity.this)
//                                .insertDeviceList(date,
//                                        device.getName(),
//                                        device.getAddress());
//                        drivaceDate.clear();
//
//                        selecterFormDBAndUpdateUI();


//                        hasUpdate = false;
                    break;
            }
        }
    };


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {

       if(keyCode == event.KEYCODE_BACK)    // 点击的是返回键
       {
           if(showState == SHOW_SECOND_PAGER)    // 第二页
           {
               if(Build.VERSION.SDK_INT < 21)
               {
                   mRelativeLayout.setVisibility(View.INVISIBLE);
                   showState = SHOW_FINST_PAGER;
               }
               else
               {
                   hideTheSecondLayout();
               }
               return false;
           }
       }
        return super.onKeyDown(keyCode, event);
    }


//    /**
//     * 当设备断开是执行此回调方法
//     */
//    @Override
//    public void OnDisconnected(LocalDeviceEntity device)
//    {
//        long time = System.currentTimeMillis();
//        Date date = new Date();
//        date.setTime(time);
//        LocalDataBaseUtils.getInstance(DeviceAmdActivity.this)
//                .insertDeviceList(date,
//                        device.getName(),
//                        device.getAddress(),
//                        MyConfingInfo.DISCONNECTE);
//        drivaceDate.clear();
//
//        selecterFormDBAndUpdateUI();
//    }
}
